﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace BurnSystems.Gpx
{
    /// <summary>
    /// A Track Segment holds a list of Track Points which are logically connected in order. 
    /// To represent a single GPS track where GPS reception was lost, or the GPS receiver 
    /// was turned off, start a new Track Segment for each continuous span of track data. 
    /// </summary>
    public class GpxTrackSegment
    {
        /// <summary>
        /// Stores the node
        /// </summary>
        protected XElement node;

        /// <summary>
        /// Initializes a new instance of the GpxTrack class.
        /// </summary>
        /// <param name="node">Node to be used for track</param>
        public GpxTrackSegment(XElement node)
        {            
            this.node = node;
        }

        /// <summary>
        /// Gets a list of waypoints
        /// </summary>
        public IList<GpxWaypoint> Waypoints
        {
            get
            {
                return this.node
                    .Elements(GpxDocument.FullyQualifiedNS + "trkpt")
                    .Select(x => new GpxWaypoint(x))
                    .ToList()
                    .AsReadOnly();
            }
        }

        /// <summary>
        /// Gets the distances between the points. These distances are calculated
        /// </summary>
        public List<double> Distances
        {
            get
            {
                var result = new List<double>();

                var waypoints = this.Waypoints;

                for (var n = 0; n < waypoints.Count - 1; n++)
                {
                    result.Add(GpxPoint.GetDistance(waypoints[n], waypoints[n + 1]));
                }

                return result;
            }
        }

        /// <summary>
        /// Gets the distances between the points. These distances are calculated
        /// </summary>
        public List<TimeSpan> TimeSpans
        {
            get
            {
                var result = new List<TimeSpan>();

                var waypoints = this.Waypoints;

                for (var n = 0; n < waypoints.Count - 1; n++)
                {
                    result.Add(waypoints[n + 1].Time - waypoints[n].Time);
                }

                return result;
            }
        }

        /// <summary>
        /// Gets the bounding box of the tracksegment
        /// </summary>
        public BoundingBox BoundingBox
        {
            get
            {
                var waypoints = this.Waypoints;

                if (this.Waypoints.Count == 0)
                {
                    // No boundingbox
                    return null;
                }

                var bb = new BoundingBox();
                bb.Right = bb.Left = waypoints[0].Longitude;
                bb.Bottom = bb.Top = waypoints[0].Latitude;

                for (var n = 1; n < waypoints.Count; n++)
                {
                    bb.Add(waypoints[n].Longitude, waypoints[n].Latitude);
                }

                return bb;
            }
        }
    }
}
